using UnityEngine;
using System.Collections;
using System.Collections.Generic;



public class AnimateWhenNearAndSeen : KnowsWhenSeen
{
	public GameObjAndAnim[] m_AnimationsToPlayWhenIAmSeen; 
	public bool Seen;

	void Start()
	{
		Start_For_KnowsWhenSeen();
		
		foreach( GameObjAndAnim ga in m_AnimationsToPlayWhenIAmSeen )
		{
			ga.m_Hash = Animator.StringToHash( ga.m_AnimationName );
			ga.m_Animator = ga.m_GameObjToAnimate.GetComponent<Animator>();
		}
	}

	override public void Saw()
	{
		foreach( GameObjAndAnim ga in m_AnimationsToPlayWhenIAmSeen )
		{
			ga.m_Animator.Play( ga.m_Hash );

			if (ga.m_AudioClip != null ) 
			{
				PlaySound( ga.m_AudioClip );
			}
		}
	}
	
	override public void LookedAway()
	{
		foreach( GameObjAndAnim ga in m_AnimationsToPlayWhenIAmSeen )
		{
			//Animation anim = ga.m_GameObjToAnimate.animation;
			//anim[ ga.m_AnimationName ].wrapMode = WrapMode.Once;
			//anim.Play( ga.m_AnimationName );
		}
//		if ( m_ResetOnLookAway && m_PauseTilSeen )
//		{
//			m_currStepIdx = -1;
//			GotoNextStep();
//		}
	}
	
	void Update() 
	{
		//Seen
		Update_For_KnowsWhenSeen();
//
//		if (   m_PauseTilSeen   &&   ( ! CanIBeSeen() )   )
//		{
//			return; // We care that we are seen and are not seen, so exit Update here
//		}
//		else
//		{
//			if ( m_currCountdown > 0 )
//			{
//				m_currCountdown -= Time.deltaTime;
//				if ( m_currCountdown <= 0f )
//				{
//					GotoNextStep();
//				}
//			}
//		}
	}

	void PlaySound( AudioClip ac )
	{
		if ( audio == null )
		{
			AudioSource.PlayClipAtPoint( ac, Camera.main.transform.position );
		}
		else
		{
			audio.PlayOneShot( ac );
		}
	}

}

[System.Serializable]
public class GameObjAndAnim
{
	public GameObject m_GameObjToAnimate;
	public string m_AnimationName;
	public AudioClip m_AudioClip;

	private int m_hash;
	public int m_Hash { get {return m_hash; } set { m_hash = value; } }

	private Animator m_anim;
	public Animator m_Animator { get {return m_anim; } set { m_anim = value; } }
}